package com.cskaoyan.gateway.controller.order;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.order.OrderCloseService;
import com.mall.order.OrderCoreService;
import com.mall.order.OrderEndService;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dto.*;
import com.mall.user.intercepter.TokenIntercepter;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/shopping")
public class OrderController {

    @Reference(timeout = 3000, check = false, retries = 0)
    private OrderCoreService orderCoreService;
    @Reference(timeout = 3000, check = false, retries = 0)
    private OrderQueryService orderQueryService;
    @Reference(timeout = 3000, check = false, retries = 0)
    private OrderCloseService orderCloseService;
    @Reference(timeout = 3000, check = false, retries = 0)
    private OrderEndService orderEndService;

    @PostMapping("order")
    public ResponseData insertOrder(@RequestBody CreateOrderRequest createOrderRequest, HttpServletRequest request) {
        String userInfo = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());
        createOrderRequest.setUserId(uid);

        try {
            CreateOrderResponse order = orderCoreService.createOrder(createOrderRequest);
            if (order.getCode().equals(OrderRetCode.SUCCESS.getCode()))
                return new ResponseUtil<String>().setData(order.getOrderId());
            return new ResponseUtil<>().setErrorMsg(Integer.valueOf(order.getCode()), order.getMsg());
        }catch (Exception e){
            return new ResponseUtil<>().setErrorMsg(Integer.valueOf(OrderRetCode.DB_EXCEPTION.getCode()),"商品库存未初始化");
        }
    }

    @GetMapping("order")
    public ResponseData queryAllOrder(HttpServletRequest request, Integer size, Integer page, String sort) {
        //通过request拿用户的id
        String userInfo = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());


        OrderListRequest orderListRequest = new OrderListRequest();
        orderListRequest.setUserId(uid);
        orderListRequest.setSize(size);
        orderListRequest.setPage(page);
        orderListRequest.setSort(sort);
        OrderListResponse listResponse = orderQueryService.queryAllOrder(orderListRequest);
        if (listResponse.getCode().equals(OrderRetCode.SUCCESS.getCode()))
            return new ResponseUtil<>().setData(listResponse);
        return new ResponseUtil<>().setErrorMsg(Integer.valueOf(listResponse.getCode()), listResponse.getMsg());
    }

    @GetMapping("order/{id}")
    public ResponseData queryOrderDetail(@PathVariable("id") String orderId) {
        OrderDetailRequest orderDetailRequest = new OrderDetailRequest();
        orderDetailRequest.setOrderId(orderId);
        OrderDetailInfoResponse orderDetailInfoResponse = orderQueryService.queryOrderDetail(orderDetailRequest);
        if (orderDetailInfoResponse.getCode().equals(OrderRetCode.SUCCESS.getCode()))
            return new ResponseUtil<>().setData(orderDetailInfoResponse);
        return new ResponseUtil<>().setErrorMsg(Integer.valueOf(orderDetailInfoResponse.getCode()), orderDetailInfoResponse.getMsg());
    }

    @DeleteMapping("order/{id}")
    public ResponseData deleteOrder(@PathVariable("id") String id) {
        DeleteOrderRequest deleteOrderRequest = new DeleteOrderRequest();
        deleteOrderRequest.setOrderId(id);
        DeleteOrderResponse deleteOrderResponse = orderEndService.deleteOrder(deleteOrderRequest);
        if (deleteOrderResponse.getCode().equals(OrderRetCode.SUCCESS.getCode()))
            return new ResponseUtil<>().setData(deleteOrderResponse);
        return new ResponseUtil<>().setErrorMsg(Integer.valueOf(deleteOrderResponse.getCode()), deleteOrderResponse.getMsg());
    }

    @PostMapping("cancelOrder")
    public ResponseData cancelOrder(@RequestBody Map<String, String> orderId) {
        CancelOrderRequest cancelOrderRequest = new CancelOrderRequest();
        cancelOrderRequest.setOrderId(orderId.get("orderId"));
        CancelOrderResponse cancelOrderResponse = orderEndService.cancelOrder(cancelOrderRequest);
        if (cancelOrderResponse.getCode().equals(OrderRetCode.SUCCESS.getCode()))
            return new ResponseUtil<>().setData(cancelOrderResponse);
        return new ResponseUtil<>().setErrorMsg(Integer.valueOf(cancelOrderResponse.getCode()), cancelOrderResponse.getMsg());
    }
}